﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using TTBusiness.DB_Linq;
namespace TTBusiness.ClassDB
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsBSOrderDetail
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblOrderDetail> getAll()
        {
            try
            {
                var list = from a in objDB.tblOrderDetails
                           orderby a.ID descending
                           select a;
                return list.ToList<tblOrderDetail>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblOrderDetail> getByOrder(int orderId)
        {
            try
            {
                var list = from a in objDB.tblOrderDetails
                           where a.OrderID.Equals(orderId)
                           orderby a.ID descending
                           select a;
                return list.ToList<tblOrderDetail>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public tblOrderDetail getById(int id)
        {
            try
            {
                var obj = objDB.tblOrderDetails.SingleOrDefault(a => a.ID == id);
                return obj;
                
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void insert(tblOrderDetail obj)
        {
            try
            {
                objDB.tblOrderDetails.InsertOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void update(tblOrderDetail obj)
        {
            try
            {
                var objj = objDB.tblOrderDetails.SingleOrDefault(a => a.ID == obj.ID);
                if (objj != null)
                {
                    objj.ID = obj.ID;
                    objj.Count = obj.Count;
                    objj.OrderID = obj.OrderID;
                    objj.Price = obj.Price;
                    objj.ProductID = obj.ProductID;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        //public void update(int id, bool status)
        //{
        //    try
        //    {
        //        var obj = objDB.tblOrders.SingleOrDefault(a => a.ID == id);
        //        if (obj != null)
        //        {
        //            obj.Status = status;
        //            objDB.SubmitChanges();
        //        }
        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //}
        public void delete(int id)
        {
            try
            {
                var obj = (from a in objDB.tblOrderDetails
                           where a.ID == id
                           select a).SingleOrDefault();
                objDB.tblOrderDetails.DeleteOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
